package cn.wolfcode.web.interceptor;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.util.UserContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class PermissionInterceptor implements HandlerInterceptor {

   /* public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
        //权限判断逻辑
        //1.获取当前登录用户
        Employee employee = UserContext.getCurrentUser();
        //2.判断用户是否是超级管理员
        if (employee.isAdmin()) {
            //是:放行
            return true;
        }
        //否:3
        //3.获取当前要执行的处理方法(controller中具体要执行的方法)
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        RequiredPermission annotation = handlerMethod.getMethodAnnotation(RequiredPermission.class);
        //4.判断该方法是否有贴自定义的权限注解
        if (annotation == null) {
            //否-->方行
            return true;
        }


        //是-->5
        //5.获取该方法的权限表达式
        String expression = annotation.expression();
        //6.获取当前用户已经拥有的权限表达式集合
        List<String> permissionExperssions = UserContext.getExpression();

        //List<String> permissionExperssions = permissionService.selectByEmpId(employee.getId());


        //7.判断方法的权限表达式是否在该集合中
        if (permissionExperssions.contains(expression)) {
            //是-->放行
            return true;
        }
        //否-->8
        //8.跳转到错误提示的页面
        request.getRequestDispatcher("/WEB-INF/views/common/nopermission.jsp").forward(request,response);
        //9.不放行
        return false;
    }*/
}
